package com.softWang.system.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.softWang.common.utils.excel.dict.DictDataApi;
import com.softWang.common.utils.excel.dict.DictDataRespDTO;
import com.softWang.system.mapper.SystemDictDataMapper;
import com.softWang.system.po.SystemDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictDataApiImpl implements DictDataApi {
    @Resource
    private SystemDictDataMapper systemDictDataMapper;
    @Override
    public DictDataRespDTO getDictData(String type, String value) {
        SystemDictData data = systemDictDataMapper.selectOne(new LambdaQueryWrapper<SystemDictData>()
                .eq(SystemDictData::getDictKey, type)
                .eq(SystemDictData::getValue, value));
        if (data == null) {
            return null;
        }
        DictDataRespDTO respDTO = new DictDataRespDTO();
        respDTO.setDictType(data.getDictKey());
        respDTO.setValue(data.getValue());
        respDTO.setLabel(data.getLabel());
        respDTO.setStatus(data.getState());
        return respDTO;
    }

    @Override
    public DictDataRespDTO parseDictData(String type, String label) {
        SystemDictData data = systemDictDataMapper.selectOne(new LambdaQueryWrapper<SystemDictData>()
                .eq(SystemDictData::getDictKey, type)
                .eq(SystemDictData::getLabel, label));
        if (data == null) {
            return null;
        }
        DictDataRespDTO respDTO = new DictDataRespDTO();
        respDTO.setDictType(data.getDictKey());
        respDTO.setValue(data.getValue());
        respDTO.setLabel(data.getLabel());
        respDTO.setStatus(data.getState());
        return respDTO;
    }

    @Override
    public List<DictDataRespDTO> getDictDataList(String dictType) {
        List<DictDataRespDTO> results = new ArrayList<>();
        List<SystemDictData> dictDataList = systemDictDataMapper.selectList(
                new LambdaQueryWrapper<SystemDictData>()
                        .eq(SystemDictData::getDictKey, dictType)
            );
        if (dictDataList != null && dictDataList.size() > 0) {
            results = dictDataList.stream().map(item -> {
                DictDataRespDTO result = new DictDataRespDTO();
                result.setLabel(item.getLabel());
                result.setValue(item.getValue());
                result.setStatus(item.getState());
                result.setDictType(item.getDictKey());
                return result;
            }).collect(Collectors.toList());
        }
        return results;
    }
}
